// 定义一个中间件来对客户端传递的 token 进行验证
const {verifyToken} = require('../utils/token')

module.exports = async (req, res, next) => {
  const token = req.headers.token || req.body.token;

  if (!token) {
    return res.json({code: 101, message: "请输入token"})
  }
  const user = await verifyToken(token)
  if (user) {
    // 将当前用户挂载到 req 上
    req.user = user
    next()
  } else {
    return res.json({code: 101, message: "输入token不合法"})
  }

}
